
const MD_LOGSETTING=window.MD_LOGSETTING={
    isLogging:false,//总开关
    uploadServer:{
        enable:false,
        api:''
    },
    //level:tag:page:
    showRule:'*:*:*'//字符串，方便通过url传递参数
};
window.MD_LOGSET=function(key,val){
    MD_LOGSETTING[key]=val;
    localStorage.setItem('MD_LOGSET',JSON.stringify(MD_LOGSETTING));
}
function initSetting(){
    const logSet=JSON.parse(localStorage.getItem('MD_LOGSET'));
    Object.assign(window.MD_LOGSETTING,logSet);
}

initSetting();

const {isLogging,showRule}=window.MD_LOGSETTING;

function canShow(context){
    const [rLevel,rTag,rPage]=showRule.split(':');
    const {level,tag,page}=context;

    if((rLevel===level||rLevel==='*') &&
        (rTag===tag||rTag==='*') &&
        (rPage===page||rPage==='*')
    ){
        return true;
    }
    return false;
}

function log(context,msg){
    if(!isLogging){
        return;
    }
    _log(context,msg);
}

function _log(context,msg=''){
    const {level,tag,page}=context;
    if(canShow(context)){
        let time=(new Date).toUTCString();
        console[level](`[${time} ${page} ${tag}] ${msg}`)
    }
}

function injectLog(...args){
    let [page,level,tag,msg]=args;
    if(args.length===2){
        msg=args[1];
        level='log';
        tag='-';
    }
    if(args.length===3){
        tag=args[1];
        msg=args[2];
        level='log';
    }
    log({
        level,
        tag,
        page
    },msg);
}
function logEmpty(){}

export default log;
export {
    injectLog
}